
# Application
set(_exec shader_test)
add_executable(${_exec}
    main.cpp
    ShaderReflection.h
    ShaderReflection.cpp
    d3dx12.h)

target_link_libraries(${_exec} PRIVATE Selah)

set_target_properties(${_exec} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${SELAH_RUNTIME_OUTPUT_DIRECTORY})

# Create a Falcor application and link the main Falcor library.
# function(add_falcor_executable target)
#     add_executable(${target})

#     target_link_libraries(${target} PRIVATE Falcor)

#     set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${FALCOR_RUNTIME_OUTPUT_DIRECTORY})

#     set_target_properties(${target} PROPERTIES VS_DEBUGGER_ENVIRONMENT "FALCOR_DEVMODE=1")
# endfunction()

function(add_shader_target TARGET_NAME SHADER_SOURCE)
    get_filename_component(SHADER_NAME ${SHADER_SOURCE} NAME_WE)
    get_filename_component(SHADER_EXT ${SHADER_SOURCE} EXT)
    
    # Determine shader profile based on file extension
    if(SHADER_EXT STREQUAL ".vert")
        set(SHADER_PROFILE "vs_6_0")
    elseif(SHADER_EXT STREQUAL ".frag")
        set(SHADER_PROFILE "ps_6_0")
    elseif(SHADER_EXT STREQUAL ".comp")
        set(SHADER_PROFILE "cs_6_0")
    else()
        message(FATAL_ERROR "Unsupported shader type: ${SHADER_EXT}")
    endif()
    
    set(OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${SHADER_NAME}.cso")
    set(DEPS_FILE "${CMAKE_CURRENT_BINARY_DIR}/${SHADER_NAME}.d")
    
    add_custom_command(
        OUTPUT ${OUTPUT_FILE}
        COMMAND ${DXC_EXECUTABLE}
            -T ${SHADER_PROFILE}
            -E main
            -Fo ${OUTPUT_FILE}
            -MF ${DEPS_FILE}
            ${CMAKE_CURRENT_SOURCE_DIR}/${SHADER_SOURCE}
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SHADER_SOURCE}
        DEPFILE ${DEPS_FILE}
        COMMENT "Compiling shader: ${SHADER_SOURCE}"
        VERBATIM
    )
    
    add_custom_target(${TARGET_NAME} DEPENDS ${OUTPUT_FILE})
endfunction()